package com.tuanbiao.dialog;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatTextView;

import com.tuanbiao.R;
import com.tuanbiao.widget.ClearEditText;

/**
 * @author abiao
 * @date 2022/10/27 9:49
 * @description:
 */
public class InsertZBDialog extends BaseDialogFragment {

    private static final String KEY_TO_QUERY_ITEM = "_KEY_TO_QUERY_ITEM_";
    private AppCompatTextView tvTitle;
    private ClearEditText cetName;
    private ClearEditText cetIp;
    private ClearEditText cetPort;
    private AppCompatButton btnCancel;
    private AppCompatButton btnConfirm;
    private OnConfirmListener onConfirmListener;

    public InsertZBDialog setOnConfirmListener(OnConfirmListener onConfirmListener) {
        this.onConfirmListener = onConfirmListener;
        return this;
    }

    public static InsertZBDialog newInstance() {
        Bundle args = new Bundle();
        InsertZBDialog fragment = new InsertZBDialog();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initViews(View view) {
        tvTitle = (AppCompatTextView) view.findViewById(R.id.tv_title);
        cetName = (ClearEditText) view.findViewById(R.id.cet_name);
        cetIp = (ClearEditText) view.findViewById(R.id.cet_ip);
        cetPort = (ClearEditText) view.findViewById(R.id.cet_port);
        btnCancel = (AppCompatButton) view.findViewById(R.id.btn_cancel);
        btnConfirm = (AppCompatButton) view.findViewById(R.id.btn_confirm);
        btnCancel.setOnClickListener(v -> {
            if (null != onConfirmListener) {
                onConfirmListener.onCancel();
            }
            dismiss();
        });
        btnConfirm.setOnClickListener(v -> {
            if (null == onConfirmListener) {
                return;
            }
            String name = getString(cetName);
            String ip = getString(cetIp);
            String port = getString(cetPort);
            if (TextUtils.isEmpty(name)) {
                showShort("请输入名称");
                return;
            }

            if (TextUtils.isEmpty(ip)) {
                showShort("请输入中爆网地址");
                return;
            }

            if (TextUtils.isEmpty(port)) {
                showShort("请输入端口号");
                return;
            }
            onConfirmListener.onConfirm(name, ip, port);
            dismiss();
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_insert_zb;
    }

    public interface OnConfirmListener {
        default void onCancel() {

        }

        void onConfirm(String name, String ip, String port);
    }
}